package com.core.safe;

import com.core.comment.StringUtil;
import com.core.netty.http.Intercept.MyInterceptFactory;
import com.core.spring.DefaultMyApplication;
import com.core.spring.IocBeanFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SafeCache {
    private static AuthRealm authRealm;
    private static SafeFilter filter;
    private static Map<String, SafeUserInfo> tokenUserList = new ConcurrentHashMap<>();
    private static Map<String, SafeOterInfo> roleList = new ConcurrentHashMap<>();
    private static Map<Long, String> useridToken = new ConcurrentHashMap<>();
    private static long validHourLength = 2;
    private static long validTimeLength = validHourLength * 3600 * 1000;

    public static AuthRealm getAuthRealm() {
        return authRealm;
    }

    public static void setAuthRealm(AuthRealm auth) {
        authRealm = auth;
    }

    public static SafeFilter getFilter() {
        return filter;
    }

    public static void setFilter(SafeFilter fil) {
        filter = fil;
    }

    public static String addUser(SafeUserInfo user) {
        String token = StringUtil.getUuid();
        user.setToken(token);
        tokenUserList.put(token, user);
        if (user.getId() != null) {
            String oldToken = useridToken.get(user.getId());
            delUser(oldToken);
            useridToken.put(user.getId(), token);
        }
        return token;
    }

    public static SafeUserInfo getUser(String token) {
        SafeUserInfo user = null;
        user = tokenUserList.get(token);
        if (user != null && user.getSavetime() > (new Date().getTime() + validTimeLength)) {
            delUser(token);
            user = null;
        }
        return user;
    }

    private static void delUser(String token) {
        if (StringUtil.isNotBlack(token)) {
            tokenUserList.remove(token);
            roleList.remove(token);
        }

    }

    public static void addUserRole(String token, SafeOterInfo role) {

        roleList.put(token, role);

    }

    public static SafeOterInfo getUserRole(String token) {
        SafeOterInfo role = null;
        role = roleList.get(token);

        return role;
    }

    private static void initTool(Object o, Method authMethod, Method filterMethod) {
        Object[] arg = new Object[0];
        try {
            Object auth = authMethod.invoke(o, arg);
            setAuthRealm((AuthRealm) auth);
            Object filter = filterMethod.invoke(o, arg);
            setFilter((SafeFilter) filter);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void init() {
        //添加安全拦截器
        MyInterceptFactory.addClass(MySafeIntercept.class);
        Class clazz = ((DefaultMyApplication) IocBeanFactory.getInstant()).getConfigClass();

        Method authMethod = null;
        Method filterMethod = null;
        if (clazz == null) return;
        for (Method m : clazz.getMethods()) {
            if (m.getReturnType().equals(AuthRealm.class)) {
                authMethod = m;
            } else if (m.getReturnType().equals(SafeFilter.class)) {
                filterMethod = m;
            }
        }
        if (authMethod == null || filterMethod == null) {
            throw new RuntimeException("myshir没有配置完");
        }
        try {
            Object o = clazz.newInstance();
            initTool(o, authMethod, filterMethod);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


    }

}
